﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QuanLyKhachSan
{
    public partial class frmTraCuuPhong : Form
    {
       
        public frmTraCuuPhong()
        {
            InitializeComponent();
        }

        //bien luu he thong hien tai
        private System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;

        private void frmTraCuuPhong_Load(object sender, EventArgs e)
        {
            //Lấy giờ hệ thống
            string gio = DateTime.Now.ToLongTimeString();
            string ngay = DateTime.Now.ToLongDateString();
            string chuoi = ngay + " " + gio;
            txtNgayHienTai.Text = chuoi;

            // Hien thi thong tin cac loai phong
            DataTable dt = LoaiPhongBUS.LayDanhSachLoaiPhong();
            int i;
            List<LoaiPhongDTO> dsLoaiPhong = new List<LoaiPhongDTO>();

            if (dt.Rows.Count > 0)
            {
                LoaiPhongDTO dto = new LoaiPhongDTO();
                dto.TenLoaiPhong = "Chọn Loại Phòng";
                dto.MaLoaiPhong = 0;
                dto.DonGia = 0;
                dsLoaiPhong.Add(dto);
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];

                    LoaiPhongDTO loaiPhongdto = new LoaiPhongDTO();
                    loaiPhongdto.MaLoaiPhong = int.Parse(dr["MaLoaiPhong"].ToString());
                    loaiPhongdto.TenLoaiPhong = dr["TenLoaiPhong"].ToString();
                    loaiPhongdto.DonGia = float.Parse(dr["DonGia"].ToString());

                    dsLoaiPhong.Add(loaiPhongdto);
                }
                cboLoaiPhong.DisplayMember = "TenLoaiPhong";
                cboLoaiPhong.ValueMember = "MaLoaiPhong";
                cboLoaiPhong.DataSource = dsLoaiPhong;
            }
            //su dung he thong en-US
            //oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            
        }

        public LoaiPhongDTO LayThongTinLoaiPhong()
        {
           LoaiPhongDTO loaiPhongdto=new LoaiPhongDTO();
            string chuoi="Chọn Loại Phòng";
            string donGia = txtDonGia.Text;
            if (cboLoaiPhong.Text == chuoi)
            {
                if (string.IsNullOrEmpty(txtDonGia.Text) == false)
                {
                    loaiPhongdto = LoaiPhongBUS.LayLoaiPhongTheoDonGia(donGia);

                }
                else
                    loaiPhongdto.TenLoaiPhong = "";
            }
            else
            {
                loaiPhongdto = (LoaiPhongDTO)cboLoaiPhong.SelectedItem;
            }
            return loaiPhongdto;
        }
        public PhongDTO LayThongTinPhong()
        {
            PhongDTO phongdto=new PhongDTO();
            phongdto.TenPhong = txtTenPhong.Text;
            if (rdbConTrong.Checked == true)
                phongdto.TinhTrang = 0;
            else if (rdbDaThue.Checked == true)
                phongdto.TinhTrang = 1;
            return phongdto ;
        }
        private void btnTraCuu_Click(object sender, EventArgs e)
        {
           
            // Lay thong tin tu form
            LoaiPhongDTO loaiPhongdto = new LoaiPhongDTO();
            PhongDTO phongdto = new PhongDTO();
            loaiPhongdto = LayThongTinLoaiPhong();
            phongdto = LayThongTinPhong();
            if (string.IsNullOrEmpty(txtDonGia.Text) == false)// && loaiPhongdto.DonGia != int.Parse(txtDonGia.Text))
            {               
                loaiPhongdto.DonGia = float.Parse(txtDonGia.Text);
            }
            // Xu ly tim kiem
            DataTable dt = new DataTable();
            dt = PhongBUS.DanhSachPhongTraCuu(loaiPhongdto, phongdto);

            dgvDanhSachPhong.Rows.Clear();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];

                    dgvDanhSachPhong.Rows.Add();
                    dgvDanhSachPhong.Rows[i].Cells["STT"].Value = (i+1).ToString();
                    dgvDanhSachPhong.Rows[i].Cells["phong"].Value = dr["tenPhong"].ToString();
                    dgvDanhSachPhong.Rows[i].Cells["loaiPhong"].Value = dr["tenLoaiPhong"].ToString();
                    dgvDanhSachPhong.Rows[i].Cells["donGia"].Value = dr["donGia"].ToString();
                    int tinhTrang = int.Parse(dr["tinhTrang"].ToString());
                    string tinhTrangPhong = "";
                    if (tinhTrang == 0)
                    {
                        tinhTrangPhong = "Trống";
                    }
                    else
                        tinhTrangPhong = "Đã Thuê";
                    dgvDanhSachPhong.Rows[i].Cells["tinhTrang"].Value = tinhTrangPhong;
                }
            }
            
            else
            {
                dgvDanhSachPhong.Rows.Clear();
                MessageBox.Show("Khong Tim Thay Thong Tin Cua Phong Theo Cac Yeu Cau", "Thong Bao");

            }
          
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            dgvDanhSachPhong.Rows.Clear();
            cboLoaiPhong.SelectedIndex = 0;
            txtDonGia.Text = "";
            txtTenPhong.Text = "";
            if (rdbConTrong.Checked == true)
            {
                rdbConTrong.Checked = false;
                rdbDaThue.Checked = true;
            }
            if (rdbDaThue.Checked == true)
            {
                rdbDaThue.Checked = false;
                rdbConTrong.Checked = true;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtDonGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void cboLoaiPhong_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = cboLoaiPhong.SelectedIndex;
            if (index >= 1)
            {
                LoaiPhongDTO lp = (LoaiPhongDTO)cboLoaiPhong.SelectedItem;
                lp = LoaiPhongBUS.LayLoaiPhongTheoMa(lp.MaLoaiPhong);
                txtDonGia.Text = lp.DonGia.ToString();
                txtDonGia.Enabled = false;
            }
            if (index == 0)
            {
                txtDonGia.Text = "";
                txtDonGia.Enabled = true;
            }
        }

        private void frmTraCuuPhong_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
        }
        
    }
}